草庐IT

C++11 auto 和 size_type

全部标签

c++ - 使用 C++11 std::condition_variable 的 Gtest 意味着 valgrind 错误

如果我以这种方式使用google测试框架编写测试:TEST_F(TestFName,TestName){std::condition_variablecv;}它会生成一个valgrind错误。我使用--leak-check=full--track-origins=yes选项运行它。Conditionaljumpormovedependsonuninitialisedvalue(s)==17215==at0x4E3DA82:pthread_cond_destroy@@GLIBC_2.3.2(pthread_cond_destroy.c:35)...Uninitialisedvaluewa

c++ - 试图理解 auto_ptr

我试图了解有关auto_ptr类如何工作的某些细节。假设您有以下类(class)(我是在一个网站上找到的,该网站上有人解释了赋值运算符的要点)。classTFoo:publicTSuperFoo{auto_ptrfBar1;auto_ptrfBar2;public:TFoo&TFoo::operator=(constTFoo&that);//variousothermethoddefinitionsgohere...}现在是赋值运算符的实现。TFoo&TFoo::operator=(constTFoo&that){if(this!=&that){auto_ptrbar1=newTBar

c++ - 将 C++11 lambda 与 boost::multi_index 结合使用

尝试使用C++11lambda作为boost::multi_index的关键访问器:#include#include#includestructFoobar{intkey;};voidfunc(){namespacemii=boost::multi_index;typedefboost::multi_index_container>>Container;}但是从g++4.8.2和boost1.53得到编译错误:error:couldnotconverttemplateargument'func()::__lambda0{}'to'int(*)(constFoobar&)'这个答案Usi

c++ - Visual Studio 2010 C++ : malloc()/HeapAlloc always allocates new page for even smallest alloc size

我有一个相当大的C/C++项目,我一直在试图找出它消耗过多内存的原因(通过任务管理器中的“工作集”判断)。我终于找到了奇怪的行为,即使是最小的malloc()请求,它也会分配一个全新的4k页面。像这样的代码for(intbla=0;bla这应该会增加区区10KB的内存消耗,最终会增加4MB,因为它分配了1000个4kB。真正令人沮丧的部分是我无法将其作为独立文件重现。仅包含上述代码的小型应用程序就可以正常工作。只有大项目才会表现出错误的行为。直接回答一些明显的建议:我正在引入与大项目相同的库,并确保编译标志相同"new"的行为方式相同在Debug和Release模式下都会发生我真的追踪

c++ - 为什么不能形成对 'decltype(auto)' 的引用

intmain(){decltype(auto)&&a=100;}以上代码,在GCC和Clang中出错。intmain(){decltype(int)&&a=100;}此代码正确。在N4296中,在§8.3.2/6Ifatypedef(7.1.3),atypetemplate-parameter(14.3.1),oradecltype-specifier(7.1.6.2)denotesatypeTRthatisareferencetoatypeT,anattempttocreatethetype“lvaluereferencetocvTR”createsthetype“lvaluere

c++ - 为什么存在 C++11 std::initializer_list 构造函数重载规则?

我似乎无法想到也找不到以下代码的理由:std::vectora{1,2}//calls(7)std::vectora(1,2)//calls(2)//constructorsfromhttp://en.cppreference.com/w/cpp/container/vector/vectorvector(std::initializer_listinit,constAllocator&alloc=Allocator());//(7)explicitvector(size_typecount,constT&value=T(),constAllocator&alloc=Allocator

c++ - 我们能否在运行时确定两个 type_info 是否可转换?

有没有办法从两个const::std::type_info中判断对象,让我们将它们命名为B和D如果D描述的类型是从类型B派生的?我问是因为我想删除我得到的对象的类型,但稍后能够检查它是否可以安全地提升。void*data;const::std::type_info*D;templatevoidstore(D&&object){D=&typeid(object);data=::std::addressof(object);}templateB&load(){//if(typeid(B)!=(*D))throw::std::bad_cast{};return*reinterpret_cas

C++ 单例 — 不是完整的 C++ 11 标准

我正在努力用C++设计一个线程安全的单例实现,但它主要针对VisualStudio2012环境。我知道C++11标准保证了这一点Foo&Instance(){staticFooinstance;returninstance;}是线程安全的。但是VisualStudio2012中使用的编译器还没有完全符合C++11标准(至少在静态变量初始化的线程安全方面)。所以我想到了这个:#include#include#includeclassFoo{public:staticFoo&Instance();private:Foo(){init();}Foo(Fooconst&);voidoperat

c++ - 是否对 boost 进行了优化以使用 C++11 中的可变参数模板?

我找不到关于(1)Boost是否已经在使用C++11的可变参数模板而不是使用MPL的版本中可用的信息,以及(2)在制作“典型”时可能期望减少什么样的编译时间在常见平台(MSVC2013、clang、gcc)上使用常见的Boost习语(ptr、tuple、pair、mutex等)。谢谢! 最佳答案 (Boost)Hana似乎是那个主动。许多子库选择只做他们的“下一个”版本c++11(Spirit、Fusion、Proto-0x和其他?)。所以如果你愿意的话,有一种远离MPL的运动。我不认为MPL在不久的将来会被“取代”。对于选择退出c

C++11统一初始化和函数重载

简单程序:voidf(conststd::string&s);voidf(constchar*p);f({});为什么clang调用f((constchar*)nullptr)?我已经预料到编译器会发出关于不明确调用的警告。 最佳答案 这包含在C++11标准草案13.3.3.1.5[over.ics.list]中,它说:Otherwise,iftheparametertypeisnotaclass:[...]iftheinitializerlisthasnoelements,theimplicitconversionsequence